package com.heyqing.topic.TitleToNumber_0171;

/**
 * ClassName:TitleToNumber
 * Package:com.heyqing.topic.TitleToNumber_0171
 * Description:
 *
 * @Date:2024/9/19
 * @Author:Heyqing
 */
public class TitleToNumber {
    public int titleToNumber(String columnTitle) {
        if (columnTitle.length() == 1) return columnTitle.charAt(0) - 64;
        int sum = columnTitle.charAt(columnTitle.length() - 1) - 64;
        for (int i = columnTitle.length() - 2; i >= 0; i--) {
            sum += (columnTitle.charAt(i) - 64) * Math.pow(26, columnTitle.length() - i - 1);
        }
        return sum;
    }
}
